import { Message } from 'element-ui'
import ipcUtil from '@/assets/utils/ipcUtil.js'
import router from './../../router'
const urlArray = ['/login', '/verify', '/registe', '/checkEmail']
const send = (uri, request) => {
  var filteredArr = urlArray.filter(item => item === uri)
  if (filteredArr.length === 0 && !localStorage.getItem('token')) {
    router.push({ path: '/' })
    return false
  } else {
    request.headers.uri = uri
    return ipcUtil.send(request).then(res => {
      if (res.code && res.code >= 400) {
        if (res.code && res.code >= 400) {
          Message({ message: res.message, type: 'error', duration: 5 * 1000 })
          throw new Error(res.message)
        }
      } else {
        return { data: res.data }
      }
    })
  }
}
export const postRequest = (uri, params) => {
  return send(uri, {
    type: 'post',
    params: params,
    headers: { token: localStorage.getItem('token') }
  })
}
export const putRequest = (uri, params) => {
  return send(uri, {
    type: 'put',
    params: params,
    headers: { token: localStorage.getItem('token') }
  })
}
export const getRequest = (uri, params) => {
  return send(uri, {
    type: 'get',
    params: params,
    headers: { token: localStorage.getItem('token') }
  })
}
export const deleteRequest = (uri, params) => {
  return send(uri, {
    type: 'delete',
    params: params,
    headers: { token: localStorage.getItem('token') }
  })
}
